<template>
  <view
    :class="[
      ns.b(),
      ns.m(modelData.id),
      ...controller.containerClass,
      ns.is('hidden', !controller.state.visible),
    ]"
  >
    <i-biz-control-shell
      :context="controller.panel.context"
      :params="controller.panel.params"
      :modelData="control.modelData"
      v-bind="control.slotProps"
    />
  </view>
</template>
<script lang="ts">
export default {
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: "shared"
  }
}
</script>
<script setup lang="ts">
import { useNamespace } from "@/use";
import { computed, inject, PropType } from "vue";
import {
  CTX,
  IControlController,
  isSimpleDataContainer,
} from "@ibiz-template/runtime";
import { IControl, IPanelCtrlPos } from "@ibiz/model-core";
import { PanelCtrlPosController } from "./i-biz-panel-ctrl-pos.controller";

// 定义组件接收的属性
const props = defineProps({
  modelData: {
    type: Object as PropType<IPanelCtrlPos>,
    required: true,
  },
  controller: {
    type: Object as PropType<PanelCtrlPosController>,
    required: true,
  },
  controls: {
    type: Array as PropType<
      {
        modelData: IControl;
        slotProps: IData;
      }[]
    >,
    default: () => [],
  },
});

// 获取命名空间
const ns = useNamespace("panel-ctrl-pos");

// 上下文里提前预告部件
const ctx = inject("ctx") as CTX;
ctx.evt.on("onRegister", (name, c) => {
  if (name === props.modelData.id) {
    props.controller.bindControl(c as IControlController);
  }
});

const control = computed(() => {
  const { modelData, slotProps } = props.controls.find(
    (ctrl) =>
      ctrl.modelData.name === props.modelData.id ||
      ctrl.modelData.id === props.modelData.id
  );
  const _props =
    props.controller.parent &&
    isSimpleDataContainer(props.controller.parent.model)
      ? { isSimple: true, data: props.controller.data }
      : {};
  return {
    modelData,
    slotProps: { ...slotProps, ..._props },
  };
});
</script>
<style lang="scss">
@import "./i-biz-panel-ctrl-pos.scss";
</style>
